<template>
    <el-header class="customHeader fixed-header">
      <div class="topBar"></div>
      <div class="headerContent">
        <div class="headerLeft">
          <img src="../../assets/whu_logo.ico" class="logo" />
          <div class="headerDivider"></div>
          <div class="titleWrapper">
            <div class="mainTitle">讯航<span class="newsText">News</span></div>
            <div class="subTitle">——武大新闻整合平台</div>
          </div>
          <el-button type="primary" size="large" class="aiChatBtn" @click="$router.push('/ai')">
            AI Chat
          </el-button>
        </div>
       
      </div>
    </el-header>
    <!-- 添加占位元素避免内容被遮挡 -->
    <div class="header-placeholder"></div>
     
  <div class="main-container">
    <div class="news-container">
      <div class="news-title">{{news.title}}</div>
      <div class="news-meta">
        <div class="meta-author">作者：{{news.author}}</div>
        <div class="meta-time">发布时间：{{ news.submitTime }}</div>
        <div class="meta-views">浏览量：{{news.pageviews}}</div>
        <div class="meta-back-home" @click="backHome()">
          <img src="../../assets/首页.png" class="home" />
        回到首页
        </div>
      </div>
      <div class="news-dividing-line"></div>
      <div class="abstract-container">
        {{news.summary}}
      </div>
   
          <div class="news-image" :style="{ backgroundImage: `url(${news.images})` }"></div>

      <div class="news-content multiline">{{news.detail}}
</div>
    </div>
    <div class="related-container">
      <div class="related-box">
        <div class="related-header">
          <div class="related-header-icon"></div>
          <div class="related-header-title">相关阅读</div>
        </div>
        <div class="related-dividing-line"></div>
        <div class="news-list">
          <div
              v-for="(item, index) in newsItems.slice(0,7)"
              :key="index"
              class="news-item"
          >
            <div class="related-content" >
              <div class="ellipsis-text" @click="toggleRelatedPage(item)">{{ item.title }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<style scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
body {
  font-family:Arial,sans-serif;
  background-color: #f5f5f5;
  color: #0e0d0d;
  line-height: 1.6;
}

:deep(.el-container) {
  padding: 0 !important;
  margin: 0 !important;
}


.banner-image img {
  display: block; /* 移除图片底部间隙 */
  margin-top: 0px;
}

.topBar {
  height: 1.875rem;
  width: 100%;
  background-color: #b6daff;
}
 .fixed-header {
  position: fixed !important;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000; /* 确保在最上层 */
  width: 100% !important;
}

/* 占位元素样式 (根据您实际的header高度调整) */
.header-placeholder {
  height: 8.125rem; /* topBar(1.875rem) + headerContent(6.25rem) */
}

/* 标题区域样式 */
.customHeader {
  border-radius: 5%;
  background-color: #ffffff; /* 背景眼色 */
  color: rgb(7, 0, 0); /* 文字颜色 */
  padding: 0 1.25rem;
  height: 6.25rem;
  display: flex;
  align-items: center;
  flex-direction: column; /* 改为垂直布局 */
  box-shadow:
    0 0.25rem 0.5rem rgba(0, 0, 0, 0.1),
    0 0.5rem 1rem rgba(0, 0, 0, 0.1),
    0 0.75rem 1.5rem rgba(0, 0, 0, 0.1); /* 多层叠加阴影，营造立体感 */
}
/* 标题内容样式 */
.headerContent {
  width: 100%;
 height: 20rem;
  display: flex;
  justify-content: space-between; /* 改为左右布局 */
  align-items: center;
}

/* logo样式 */
.logo {
  margin-left: 1.5rem;
  height: 4rem;
  width: auto;
  object-fit: contain;
}

/* 标题容器 */
.titleWrapper {
  display: flex;
  flex-direction: column;
}

/* 主标题样式 */
.mainTitle {
  font-size: 2.2rem;
  font-weight: bold;
  color: #000;
}

/* News文字样式 */
.newsText {
  margin-left: 0.3125rem;
}

/* 副标题样式 */
.subTitle {
  font-size: 1rem;
  color: #409eff; /* Element Plus 主蓝色 */
}

/* 分割线 */
.headerDivider {
  height: 4rem;
  width: 0.0625rem;
  background-color: #c9d5f1;
}

/* 新增左侧容器 */
.headerLeft {
  display: flex;
  align-items: center;
  gap: 0.9375rem;
}

/* 右侧搜索框样式 */
.headerRight {
  width: 17.5rem;
  margin-left: auto;
}

/* 输入框样式 */
.searchInput {
  :deep(.el-input__inner) {
    height: 2.5rem;
    line-height: 2.5rem;
    font-size: 1rem;
    padding: 0 0.9375rem;
    /* 确保输入区域可点击 */
    pointer-events: auto;
    cursor: text;
  }

  :deep(.el-input__wrapper) {
    /* 移除可能遮挡的样式 */
    overflow: visible;
  }
}

.search-icon {
  width: 0.9rem;
  height: 0.9rem;
  margin-bottom: 1rem;
  object-fit: contain;
}

.headerRightGroup {
  display: flex;
  align-items: center;
  gap: 1.25rem;
}

/* 管理员区域样式优化 */
.adminArea {
  display: flex;
  align-items: center;
  gap: 0.5rem; /* 缩小间距 */
  margin-left: 0.9375rem; /* 与搜索框的间距 */
}

/* 管理员按钮样式优化 */
.adminBtn {
  font-size: 1.125rem; /* 调整为0.875rem */
  font-weight: 600; /* 加粗 */
  letter-spacing: normal;
  padding: 0.4375rem 0.9375rem; /* 缩小内边距 */
  border-radius: 0.25rem;
  background-color: #3589dc; /* Element主色 */
  color: white;
  transition: all 0.3s;
}

.aiChatBtn {
  font-size: 1.125rem; /* 调整为0.875rem */
  font-weight: 600; /* 加粗 */
  letter-spacing: normal;
  padding: 0.4375rem 0.9375rem; /* 缩小内边距 */
  border-radius: 0.25rem;
  background-color: #3589dc; /* Element主色 */
  color: white;
  transition: all 0.3s;
}


.adminBtn:hover {
  background-color: #66b1ff; /* 悬停颜色 */
  transform: translateY(-0.0625rem); /* 轻微上浮效果 */
}

/* 管理员Logo样式优化 */
.adminLogo {
  width: 1.75rem;
  height: 1.75rem;
  object-fit: contain;
  border-radius: 50%; /* 圆形Logo */
  border: 0.0625rem solid #e6e6e6; /* 浅色边框 */
  padding: 0.125rem; /* 内边距 */
  background-color: white; /* 白色背景 */
  box-shadow: 0 0.0625rem 0.1875rem rgba(0, 0, 0, 0.1); /* 轻微阴影 */
}

/*
.top-background::after {
  position: absolute;
  content: "";
  width: 100%; height: 100%;
  background-image: url('@/assets/R-C.jpg');
  opacity: 0.2;
  background-size: 100% 100%;
  overflow: hidden;
  border:1px solid #797979;
  z-index: 1; 
}*/
.icon{
  margin-left:32px;
  width:70px;
  height:70px;
  background-image: url("@/assets/whu_logo.ico");
  background-size: cover;  /* 根据需求设置 */
  background-position: center;
  background-repeat: no-repeat;
}
.dividing-line{
  margin:5px 15px;
  height: 60px;
  width: 1px;
  border:1px solid #797979;
}
.main-title{
  margin:5px 9px;
  width: 80px;
  height:60px;
  font-size:25px;
  line-height: 29px;
}
.sub-title{
  width: 300px;
  height:40px;
  padding-top: 8px;
  margin-top:30px;
  margin-left:-16px;
  color: #8080FF;
  opacity: 0.86;
  font-size: 15px;
}
.main-search{
  display: flex;
  width: 350px;
  height: 50px;
  margin-left: 496px;
  margin-top: 10px;
  background: #f2f2f2;
  border: 1px solid #797979;
  border-radius: 40px;
}
.search-box{
  width: 100%;
  height: 100%;
  padding:10px 30px;
  color: #555555;
  font-size: 15px;
  opacity: 0.8;
}
.search-icon{
  margin-top: 10px;
  margin-right: 18px;
  height: 30px;
  width:30px;
  background:url('@/assets/搜索.png') no-repeat center;
  background-size: cover;  /* 根据需求设置 */
}
.search-icon:hover{
  background-color: rgba(43, 84, 131, 0.1);
}
.custom-input >>> .el-input__wrapper {
  border: none;
  box-shadow: none;
  background-color: transparent; /* 继承外部容器背景色 */
}

.custom-input >>> input {
  background-color: transparent;
  color: #333; /* 字体颜色 */
}
.main-container{
  line-height: 28px;
  background-color: #F2F2F2;
  width: 100%;
  display: flex;

}
.news-container{
  min-height: 500px;
  background-color: white;
  width: 1200px;
  padding-left:80px;
}
.news-title{
  margin-left: -4rem;
  font-weight: bold;
  padding-top: 1.3125rem; /* 21px/16 */
  width: 100%;
  height: 6.25rem; /* 100px/16 */
  text-align: center;
  font-size: 1.4rem; /* 37px/16 */
}
.news-meta{
  font-size: 0.8125rem;
  color:grey;
  display: flex;
  height:17px;

}
.multiline {
  white-space: pre-wrap;
}
.meta-author{
  width: 250px;
}
.meta-time{
 
  width: 300px;
}
.meta-views{
  margin-left: 20px;
  width: 150px;
}
.meta-back-home{

  font-size: 18px;
  margin-left: 400px;
  cursor: pointer;
  width: 300px;
}
.home{
  margin-bottom: -3px;
  width: 20px;
  height: 20px;
}

.meta-back-home:hover{
  color:#8080FF;
  text-decoration-line: underline;
  text-decoration-color: #8080FF;
}
.news-dividing-line{

  margin-top: 22px;
  width:1000px;
  height: 1px;
  border:1px solid #797979;
}
.abstract-container{
  width: 1000px;
  font-size: 18px;
  margin-top:23px;
}
.news-image{
  margin-left: 240px;
  width: 500px;
  height:400px;
  align-items: center;
  //background-image: url("@/assets/DF42FD3E2DA95209D3E3D5394B6_7FF55428_71207.jpg");
  background-size: contain;  /* 根据需求设置 */
  background-position: center;
  background-repeat: no-repeat;
}
.news-content{
  width: 1000px;
  font-size: 18px;
}
.related-container{
  margin-left: 5%;
  display: flex;
  width: 489px;
}
.related-box{
  margin-top: 100px;
  margin-left: 100px;
  width: 288px;
  height: 400px;
  border: 1px solid #797979;
  border-radius: 20px;
  background-color: white;
}
.related-header{
  margin:30px 25px;
  display: flex;
  font-size: 20px;
}
.related-header-title{
  margin-left: 12px;
  font-size: 20px;
}
.related-header-icon{
  height: 30px;
  width: 30px;
  background-image: url("@/assets/书籍 (2).png");
  background-size: cover;  /* 根据需求设置 */
  background-position: center;
  background-repeat: no-repeat;
}
.related-dividing-line{
  margin-left: 29px;
  border: 1px solid #797979;
  height: 1px;
  width: 228px;
}
.news-list{
  margin-left: 29px;
  width: 228px;
  height: 500px;
}
.related-content{
  width: 200px;
  font-size: 14px;
}
.news-item {
  display: flex;
  align-items: center;
  padding: 5px 20px;
  background: #ffffff;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  cursor: pointer;
  border-left: 4px solid transparent;
  border-bottom: 1px solid rgb(128, 128, 128);
  border-radius: 10px;
}
.news-item:hover {
  text-decoration: underline;
}
.ellipsis-text {
  //display: inline-block;
  max-width: 10em;             /* 每个汉字大约占1em宽度 */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  //font-size: 16px;
  //line-height: 1.4;
}
</style>
<script>
import { ref } from 'vue'
import { reactive, onMounted } from 'vue'
const searchText = ref('')
import axios from 'axios';
//import {useRoute} from "vue-router";
import {createRouter as $router, useRoute} from "vue-router";
import {ElMessage} from "element-plus";
import router from "@/router/index.js";
const route = useRoute();

const newsItems = ref([
  {
    id: '1',
    title: '无相关信息',
  },
  {
    id: '2',
    title: '无相关信息',
  },
  {
    id: '3',
    title: '无相关信息',
  },
  {
    id: '4',
    title: '无相关信息',
  },
  {
    id: '5',
    title: '无相关信息',
  }
])
const images = [
  new URL('', import.meta.url).href,
  new URL('', import.meta.url).href,
  new URL('', import.meta.url).href
]
export default {
  setup() {
        const route = useRoute();
        const id = route.query.id ? route.query.id : 263;
        const news = reactive({
          id: id,
          title: '',
          detail: '',
          author: '',
          images: '',
          typeId: 0,
          ishot: false,
          summary:'',
          submitTime: '',
          pageviews: 0,
          source: ''
        })
        onMounted(async () => {
          const res = await axios.get('http://localhost:8080/news/'+news.id)
          Object.assign(news, res.data.data)
          const rel = await axios.get('http://localhost:8080/news/type', {
          params: {
            current: 1,
            tid: news.typeId
            }
          })
          console.log("res.data.data:",res.data)
          console.log("rel.data.data:",rel.data.data)
          /*Object.assign(newsItems, rel.data.data)*/
          const data = rel.data.data  // 假设是数组 [{id, title}, ...]

          if (Array.isArray(data) && data.length > 0) {
            newsItems.value = data
          } else {
            console.warn('后端返回数据为空，保留默认值')
          }
          console.log("newsItem:",newsItems)
          if (Array.isArray(news.images) && data.length > 0) {
            images.value = data
          } else {
            console.warn('后端返回数据为空，保留默认值')
          }
    })


    return {
      newsItems,// 👈 一定要 return 出去
      news,
      images
    }
  },
  data() {

  },
  methods: {
    async togglePopularStatus(){// 切换状态
      try{
        const payload = {
          id: this.id,
          title: this.title,
          detail: this.detail,
          author: this.author,
          images: this.images,
          typeId: this.typeId,
          ishot: this.isPopular.value,
          submitTime: this.submitTime,
          summary: this.summary,
          pageviews: this.pageviews,
          source: this.source
        }
        const res = await axios.put('http://localhost:8080/news?token='+route.query.token, payload, {
          headers: {
            'Content-Type': 'application/json'
          }
        })
        if (res.data.success) {
          ElMessage.success('状态更新成功')
        } else {
          ElMessage.error('状态更新失败')
        }
      }catch (err) {
        console.error(err)
        ElMessage.error('请求失败')
        this.isPopular = !this.isPopular // 还原状态
      } finally {

      }
    },
    async toggleRelatedPage(item){
      this.news.id=item.id;
      await router.push({path:'/HomePage/DetailPage',query:{
          id:item.id,
        }}).then(() => {
        location.reload()  // 强制刷新
      })
    },
    async backHome(){
      await router.push('/HomePage')
    },
    handleSearch() {
      // 监听搜索框输入变化，您可以选择实时搜索
    },
    async onSearch() {
      if (this.searchQuery) {
        try {
          // 发起请求到后端，假设API为 /api/search
          const response = await axios.get('/api/search', {
            params: { query: this.searchQuery }
          });
          this.results = response.data; // 设置搜索结果
        } catch (error) {
          console.error("搜索失败:", error);
        }
      }
    }
  }
};
</script>
